package com.d3.pomodoro.adapter

import android.graphics.Color
import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.d3.pomodoro.R
import com.d3.pomodoro.model.Cate
import com.orhanobut.logger.Logger

//https://www.jianshu.com/p/1e20f301272e
class HomeCateAdapter(data: MutableList<Cate>) :
    BaseQuickAdapter<Cate, BaseViewHolder>(R.layout.item_home_cate, data) {

    private var cateClickListener: CateClickListener? = null

    private var currSelectItem: Cate? = null

    override fun convert(holder: BaseViewHolder, item: Cate) {
        holder.apply {

            setVisible(R.id.itemContentView, !item.isCreateItem)
            setVisible(R.id.itemLockView, item.isCreateItem)
            if (item.isCreateItem) {
                val itemLockView = getView<LinearLayout>(R.id.itemLockView)
                itemLockView.setOnClickListener {
                    cateClickListener?.onClick(holder, item)
                }
            } else {
                setText(R.id.textTitle, item.title)
                setText(R.id.textContent, "10")
                setVisible(R.id.imageSelect, item == currSelectItem)
                setBackgroundColor(R.id.itemContentView, Color.parseColor(item.colorString))
                val itemContentView = getView<LinearLayout>(R.id.itemContentView)
                itemContentView.outlineProvider = object : ViewOutlineProvider() {
                    override fun getOutline(view: View, outline: Outline) {
                        outline.setRoundRect(0, 0, view.width, view.height, 5.toFloat())
                    }
                }
                itemContentView.clipToOutline = true

                itemContentView.setOnClickListener {
                    currSelectItem = item
                    cateClickListener?.onClick(holder, item)
                    notifyDataSetChanged()
                }
            }

        }
    }

    fun setCateClickListener(cateClickListener: CateClickListener) {
        this.cateClickListener = cateClickListener
    }


    interface CateClickListener {
        fun onClick(holder: BaseViewHolder, item: Cate)
    }

}